﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Core.General {

    public class ActionReason : Core.CoreConfigurationObject {
        
        #region Private Properties

        private Core.Entity.Enumerations.EntityType entityType = Entity.Enumerations.EntityType.NotSpecified;

        private Enumerations.ActionType actionType = Enumerations.ActionType.NotSpecified;

        #endregion 


        #region Public Properties

        public Core.Entity.Enumerations.EntityType EntityType { get { return entityType; } set { entityType = value; } }

        public Enumerations.ActionType ActionType { get { return actionType; } set { actionType = value; } }

        #endregion 

        
        #region Constructors

        public ActionReason (Application forApplication) : base (forApplication) {

            return;

        }

        public ActionReason (Application forApplication, Int64 forId) : base (forApplication, forId) {

            return;

        }
        
        public ActionReason (Application forApplication, String forName) : base (forApplication, forName) {

            return;

        }

        #endregion


        #region Database Functions

        protected override Dictionary<String, String> Validate () {

            Dictionary<String, String> validationErrors = base.Validate ();


            return validationErrors;

        }

        public override void MapDataFields (System.Data.DataRow currentRow) {
            
            base.MapDataFields (currentRow);

            EntityType = (Entity.Enumerations.EntityType) Data.Readers.DataReader.Int32FromDatabase (currentRow, "EntityType");

            ActionType = (Enumerations.ActionType) Data.Readers.DataReader.Int32FromDatabase (currentRow, "ActionType"); 

            return;

        }

        public override Boolean Save () {

            Boolean success = false;

            try {

                Dictionary<String, String> validationErrors = Validate ();

                if (validationErrors.Count > 0) {

                    Application.SetLastException (new Exception ("Invalid [" + validationErrors.Keys.First () + "]: " + validationErrors.Values.First ()));

                    return false;

                }

                UpdateModifiedToSessionUser ();


                System.Data.IDbCommand command = Application.EnvironmentDatabase.CreateCommand (ObjectType + "_InsertUpdate");

                command.CommandType = System.Data.CommandType.StoredProcedure;


                Application.EnvironmentDatabase.AppendCommandParameter (command, "Id", Id);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Name", Name, 60);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Description", Description, 999);


                Application.EnvironmentDatabase.AppendCommandParameter (command, "EntityType", ((Int32) EntityType));

                Application.EnvironmentDatabase.AppendCommandParameter (command, "ActionType", ((Int32)ActionType));


                Application.EnvironmentDatabase.AppendCommandParameter (command, "Enabled", Enabled);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "ModifiedId", Application.Session.UserAccountName, 60);

                success = (command.ExecuteNonQuery () > 0);

                if (success) { SetIdentity (); }

            }

            catch (Exception exception) {

                success = false;

                Application.SetLastException (exception);

            }

            return success;

        }

        public override Boolean Delete () {

            return base.DeleteFromDatabase (DeleteStatementById (), Application.EnvironmentDatabase);

        }

        #endregion 


    }

}
